<template>
  <!-- 单号查询面板 -->
  <div>
    <TopFilter :list="filterList" :cols="3" @filterChange="handleTopFilterChange"></TopFilter>
    <FilterTable
      ref="table"
      :columns="columns"
      columnsRef="partPartInventoryPartInventoryComponentsSearchOrder"
      :fetchapi="fetchApi"
      :params="fetchParams"
      height="200"
      selectionType="single"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="arr => (this.onChangeColumn = arr)"
    ></FilterTable>
    <!-- :onPageChange="onPageChange" -->
    <!-- :onSyncTableData="syncDataChange" -->
    <div class="btnWrap">
      <el-button @click="$emit('cancel')">取 消</el-button>
      <el-button type="primary" @click="confirmBtnClick">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { stockIn, getDealerUser } from '@/api/part/partInventory/partInventory/index.js';
import { dictionary } from '@/utils/dictMixin';
import { getUserId, getAppId } from '@/assets/js/auth';
export default {
  name: 'cgrk_searchorder',
  mixins: [dictionary],
  props: {
    stockInNo: String,
    ptype: String
  },
  data() {
    return {
      userId: '',
      columns: this.createColumns(),
      fetchApi: stockIn,
      fetchParams: { xhrAbort: true },
      onChangeColumn: [], // 选中行
      serviceAdvisorData: [], // 存人员的信息
      filterList: this.createFilterList()
    };
  },
  created() {
    this.getEmp();
    this.getUserId();
  },
  mounted() {
    // console.log(this.fetchApi,'this.fetchApi')
    this.fetchParams = { xhrAbort: false, stockInNo: this.stockInNo, ptype: this.ptype };
    console.log('cgrk_searchorder mounted', this.stockInNo);
    window.cgrk_searchorder = this;
    if (getAppId() == 'beijing') {
      this.columns = this.createColumnsBJ();
    }
  },
  methods: {
    createFilterList() {
      return [
        {
          type: 'INPUT',
          label: '入库单号',
          fieldName: 'stockInNo'
        },
        {
          type: 'INPUT',
          label: '入库类型',
          fieldName: 'stockInType',
          type: 'SELECT',
          itemList: this.createDictList(8088)
        },
        {
          type: 'INPUT',
          label: '发运单号',
          fieldName: 'purchaseCode'
        },
        {
          type: 'RANGE_DATE',
          label: '开单日期',
          fieldName: 'orderDateBeg|orderDateEnd'
        }
      ];
    },
    createColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 50,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          }
        },
        {
          title: '入库单号',
          dataIndex: 'STOCK_IN_NO',
          showOverflowTooltip: true
        },
        {
          title: '供应商代码',
          dataIndex: 'CUSTOMER_CODE'
        },
        {
          title: '供应商名称',
          width: 150,
          dataIndex: 'CUSTOMER_NAME',
          showOverflowTooltip: true
        },
        {
          title: '税率',
          dataIndex: 'TAX'
        },
        // {
        //   title: '加价率',
        //   // sorter: true,
        //   dataIndex: 'PRICE_ADD_RATE'
        // },
        {
          title: '开单日期',
          dataIndex: 'ORDER_DATE',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '经手人',
          dataIndex: 'CREATED_BY',
          render: props => {
            return <span>{this.getEmployeeName(props.row.CREATED_BY)}</span>;
          }
        },
        {
          title: '锁定人',
          dataIndex: 'LOCK_USER',
          render: props => {
            return <span>{this.getEmployeeName(props.row.LOCK_USER)}</span>;
          }
        },
        // {
        //   title: '注册号码',
        //   // sorter: true,
        //   dataIndex: 'ORDER_REGEDIT_NO'
        // },
        {
          title: '发运单号',
          dataIndex: 'PURCHASE_CODE'
        },
        {
          title: '入库类型',
          dataIndex: 'STOCK_IN_TYPE',
          filterType: 'checkbox',
          filterItems: this.createDictList(8088),
          dictItems: this.createDictList(8088)
        },
        // {
        //   title: '发运日期',
        //   // sorter: true,
        //   dataIndex: 'DELIVERY_TIME'
        // },
        {
          title: '备注',
          dataIndex: 'REMARK'
        }
      ];
    },
    createColumnsBJ() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 50,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          }
        },
        {
          title: '入库单号',
          dataIndex: 'STOCK_IN_NO',
          showOverflowTooltip: true
        },
        {
          title: '入库类型',
          dataIndex: 'STOCK_IN_TYPE',
          filterType: 'checkbox',
          filterItems: this.createDictList(8088),
          dictItems: this.createDictList(8088)
        },
        {
          title: '单据状态',
          dataIndex: 'IS_CHECKED',
          dictItems: [
            { value: 0, text: '未提交' },
            { value: 1, text: '已提交' },
            { value: 2, text: '已通过' },
            { value: 3, text: '已驳回' }
          ]
        },
        {
          title: '审核原因',
          dataIndex: 'CHECKED_RESULT'
        },
        {
          title: '供应商代码',
          dataIndex: 'CUSTOMER_CODE'
        },
        {
          title: '供应商名称',
          width: 150,
          dataIndex: 'CUSTOMER_NAME',
          showOverflowTooltip: true
        },
        {
          title: '税率',
          dataIndex: 'TAX'
        },
        // {
        //   title: '加价率',
        //   // sorter: true,
        //   dataIndex: 'PRICE_ADD_RATE'
        // },
        {
          title: '开单日期',
          dataIndex: 'ORDER_DATE',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '经手人',
          dataIndex: 'CREATED_BY',
          render: props => {
            return <span>{this.getEmployeeName(props.row.CREATED_BY)}</span>;
          }
        },
        {
          title: '锁定人',
          dataIndex: 'LOCK_USER',
          render: props => {
            return <span>{this.getEmployeeName(props.row.LOCK_USER)}</span>;
          }
        },
        // {
        //   title: '注册号码',
        //   // sorter: true,
        //   dataIndex: 'ORDER_REGEDIT_NO'
        // },
        {
          title: '发运单号',
          dataIndex: 'PURCHASE_CODE'
        },

        // {
        //   title: '发运日期',
        //   // sorter: true,
        //   dataIndex: 'DELIVERY_TIME'
        // },
        {
          title: '备注',
          dataIndex: 'REMARK'
        }
      ];
    },
    // 点击搜索或者重置
    handleTopFilterChange(e) {
      this.fetchParams.xhrAbort = true;
      this.fetchParams = Object.assign(this.fetchParams, e, { xhrAbort: false, ptype: this.ptype });
    },
    // 获取员工信息并转义
    async getEmp() {
      const res = await getDealerUser();
      this.serviceAdvisorData = res.data.map(x => ({ value: x.userId, text: x.employeeName }));
      // this.columns.find(x => x.dataIndex === 'CREATED_BY').editItems = res.data.map(x => ({ value: x.userId, text: x.employeeName }));
      // this.columns.find(x => x.dataIndex === 'LOCK_USER').editItems = res.data.map(x => ({ value: x.userId, text: x.employeeName }));
    },
    async getUserId() {
      this.userId = await getUserId();
    },
    onPageChange() {
      console.log('onPageChange');
    },
    syncDataChange(val) {},
    // 转换数据
    getEmployeeName(userId) {
      const item = this.serviceAdvisorData.find(x => {
        return x.value == userId;
      });
      return item ? item.text : '';
    },
    confirmBtnClick() {
      if (this.onChangeColumn.length == 0) {
        this.$notify({
          title: '警告',
          message: '请选择一行！',
          type: 'warning'
        });
        return;
      }
      if (this.onChangeColumn[0].LOCK_USER != this.userId && this.onChangeColumn[0].LOCK_USER != '' && this.onChangeColumn[0].LOCK_USER != null && this.onChangeColumn[0].LOCK_USER != undefined) {
        let sd = '';
        this.serviceAdvisorData.forEach(x => {
          if (x.value == this.onChangeColumn[0].LOCK_USER) {
            sd = x.text;
          }
        });
        this.$notify({
          title: '警告',
          message: '该单号已被' + sd + '锁定',
          type: 'warning'
        });
        return;
      }
      console.log('选择的单号', this.onChangeColumn[0]);
      this.$emit('confirm', this.onChangeColumn[0]);
      this.$emit('cancel');
    }
  }
};
</script>

<style scoped>
.btnWrap {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
</style>
